#include "src/spazbot.bi"

dim as string inkeyString, MOTD, nick, chanrecv, nicktosend
dim as string ircNetwork, ircChannel, toChan, chan, myChan
dim as string installpath, nul, botOwner, quitmsg
dim as integer pluginstatus
'dim as integer true, false
'
'const false = 0
'const true = 1
'
on error goto errhandler

cls
pluginstatus = true
open "config.ini" for input as #1

    do until eof(1)
        line input #1, nul
        if lcase$(nul) = "<installpath>" then
            line input #1, installpath
        elseif lcase$(nul) = "<ircnick>" then
            line input #1, nick
        elseif lcase$(nul) = "<owner>" then
            line input #1, botOwner
        elseif lcase$(nul) = "<joinnetwork>" then
            line input #1, ircNetwork
        elseif lcase$(nul) = "<joinchannel>" then
            line input #1, ircChannel
            if left$(ircChannel,1) <>"#" then
                myChan = "#" & ircChannel
            else
                myChan = ircChannel
            endif
        elseif lcase(nul) = "<quitmessage>" then
            line input #1, quitmsg
        'todo
        endif
    loop
close 1
?"Configuration loaded."

dim as sock.IRC mySock = sock.IRC( ircNetwork, nick, nick )

dim as IRC.message myMessage

dim as ubyte reloadauth, allowauth, denyauth

?"Connecting to " & ircNetwork & "..."

do 
  
  if mySock.getLine( MOTD ) then
  
    if left(MOTD,4)="PING" then MySock.PutLine( "PONG" & ( right ( MOTD, len( MOTD ) -4 )))

    if myMessage.parse( MOTD ) then
      
      dim as integer iParm = ANY
      for iParm = 1 to myMessage.params - 1
        
        ? myMessage.param( iParm ); " ";
	
      next
      ?
  
    end if 
  
   end if
  
   motd = myMessage.param( myMessage.trailing )

loop until motd = "MOTD File is missing" or motd = "End of /MOTD command."


if mySock.isClosed( ) then

  ? mySock.getError( )
  
else

  dim as string thisMessage
  
  ? "RedBall IRC Bot Engine beta"
  ?
'  ? "It's recommended that you join ""irc.freenode.net"" and channel #gurumeditation, so you can see this!"
'  ? 
'  ? "hit 'b' to test bold text"
'  ? "hit 'v' to test inverted text"
'  ? "hit 'u' to test underlined text"
'  ? "hit 'c' to test colorful text"
'  ? "hit 'r' to test rainbow text ...:P"
'  ? "hit 'n' to change nickname"
'  ? "hit 'j' to join a channel"
'  ? "hit 't' to talk! (Sends to #gurumeditation only)"
'  ? "hit 'p' to part (leave) a channel"
'  ? "hit 'r' to send rainbow text."
'  ? "hit 'w' to send a raw message."
'  ? "hit 'q' to quit the server and application."

?"Connection established."
?"Joining channel #" & myChan & "..."
toChan = myChan
mySock.joinChannel( toChan )
?"Success!"
mySock.sendMessage( myChan, "Hey guys! (Insert random crap here)" )

  do
  
    inkeyString = inkey

    '  Did we get a message?
    if mySock.getLine( thisMessage ) then
    
    myMessage.parse( thisMessage )  
 
    dim n as integer
    n=isAllowed(ltrim(rtrim(myMessage.id)))
   
    ''Send it to the plugins if the guy who sent it was authed...
    if n then
	dim msg as message
	for i as integer = 0 to numofplugins
		msg=botarray(i).BotMain(thisMessage)
		if msg.msgtype=1 then
			mySock.sendMessage( msg.dest, msg.msg )
		elseif msg.msgtype=2 then
			mySock.sendNotice ( msg.dest, msg.msg)
		elseif msg.msgtype=3 then
			mySock.PutLine( "MODE " & msg.dest & " :" & msg.msg )
		elseif msg.msgtype=4 then
			mySock.PutLine ( msg.msg )
		endif
	next
    endif

     if left(thisMessage,4)="PING" then MySock.PutLine( "PONG" & ( right ( thisMessage, len( thisMessage ) -4 )))      

      with myMessage
        
        ' Somebody messaged, and we can see it. '/
        if .command = "PRIVMSG" then
          
          ' Was that message only for us? '/
          if .param( 0 ) = "bot" Then
            
            ? "(pm) ";
          
          end if
          
          ' Who said it: What did they say? '/
          ? "<" & .id & "> " & .param( .trailing )
          
        end if
        
      end with
      
    end if

   if instr(thisMessage, "^allow") then
      if myMessage.id = botOwner then
        mySock.putLine ("WHOIS :" & myMessage.id)
        allowauth=1
        nicktosend=right(myMessage.param(myMessage.trailing), len(myMessage.param(myMessage.trailing))-7)
        mySock.sendMessage ( myChan, "Now accepting commands from " & nicktosend )
      endif
   endif
   
   if allowauth=1 and instr(thisMessage, "is identified to services") then
	AddNick(nicktosend)
        allowauth=0
   endif
   
   if instr(thisMessage, "^quit") then
      if myMessage.id = botOwner then
        mySock.sendMessage ( myChan, "Seeya everyone. Nice talking. XD" )
        goto endit
      endif
   endif
   
   if instr(thisMessage, "^remove") then
   if myMessage.id = botOwner then
        mySock.putLine ("WHOIS :" & myMessage.id)
        denyauth=1
        nicktosend=right(myMessage.param(myMessage.trailing), len(myMessage.param(myMessage.trailing))-8)
        mySock.sendMessage ( myChan, "No longer accepting commands from " & nicktosend )
      endif
   endif
   
   if denyauth=1 and instr(thisMessage, "is identified to services") then
	delNick(nicktosend)
        denyauth=0
   endif

    if inkeyString = " " or instr(thisMessage, "^reload") then 
     
      if myMessage.id = botOwner then
        mySock.putLine ("WHOIS :" & myMessage.id)
	reloadauth=1
      endif

      chanrecv=myMessage.param(0)
    
    endif

    if reloadauth=1 and instr(thisMessage, "is identified to services") then
      mySock.sendMessage( chanrecv, "Reloading plugins..." )
      dim oldtime as double
      oldtime=timer

      for i as integer = 0 to numofplugins
           dylibfree(BotArray(i).hndl)
      next
        if pluginstatus = true then
            LoadPlugins ()
        endif
      mySock.sendMessage( chanrecv, "Reloaded all plugins in " & (timer - oldtime)*1000 & " milliseconds" )
    
      reloadauth=0

    endif
    

'''___________________________________
    if inkeyString = "q" then
      input "quit message: ",quitmsg
      
      for i as integer = 0 to numofplugins
	   dylibfree(BotArray(i).hndl)
      next
           
      mySock.quit( quitmsg )
      
    end if
    
    if inkeyString = "j" then
    
      dim as string toChan
      'line input "To channel: ", toChan
       toChan = myChan
      mySock.joinChannel( toChan )
      
    end if

    if inkeyString = "p" then
    
      dim as string leaveChan, leaveMsg
      line input "Part channel: ", leaveChan
      line input "Parting msg: ", leaveMsg
    
      mySock.partChannel( leaveChan, leaveMsg )

    end if
    
    ' 20 loops a second should be plenty. '/
    sleep 50, 1

    if mySock.isClosed( ) then
      
      ' connection was kill'd '/  
      exit do
      
    end if
  
  ' loop until ESCAPE is hit '/
  loop until ( inkeyString = chr( 27 ) )

end if  

end

errhandler:
print err
sleep
end

endit:

for i as integer = 0 to numofplugins
dylibfree(BotArray(i).hndl)
next
   
mySock.quit( quitmsg )
if mySock.isClosed( ) then end
end


